package com.kaiyiweitong.tongxing.activity.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.GridLayout;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.adapter.MyOrderAdapter;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.fragment.after_sell.AfterSellFragment;
import com.kaiyiweitong.tongxing.other.SpaceItemDecoration;
import com.kaiyiweitong.tongxing.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/17 0017.
 */

public class AfterSellActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @Bind(R.id.rv_after_sell_hor)
    RecyclerView mRvAfterSellHor;
    @Bind(R.id.vp_after_sell)
    ViewPager mVpAfterSell;
    private String[] mStringArray;
    private MyOrderAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_after_sell);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mStringArray = getResources().getStringArray(R.array.after_sell);

        mRvAfterSellHor.setLayoutManager(new GridLayoutManager(this, 1, GridLayout.HORIZONTAL, false));
        mAdapter = new MyOrderAdapter(this, mStringArray, mListener, mCurrentIndex);
        mRvAfterSellHor.addItemDecoration(new SpaceItemDecoration((int) CommonUtil.getItemDecoration(this, mStringArray), 2));
        mRvAfterSellHor.setAdapter(mAdapter);

        if (mStringArray != null && mStringArray.length > 0) {
            for (int i = 0; i < mStringArray.length; i++) {
                mFragments.add(AfterSellFragment.newInstance(String.valueOf(i + 1)));
            }

            FragmentPagerAdapter pageAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
                @Override
                public int getCount() {
                    return mFragments.size();
                }

                @Override
                public Fragment getItem(int position) {
                    return mFragments.get(position);
                }
            };
            mVpAfterSell.setAdapter(pageAdapter);
            if (mStringArray.length > 2) {
                mVpAfterSell.setOffscreenPageLimit(mStringArray.length - 1);
            }
            mVpAfterSell.addOnPageChangeListener(this);

            indicator(0);
        }

    }

    private MyOrderAdapter.OnClickListener mListener = new MyOrderAdapter.OnClickListener() {
        @Override
        public void onClick(int position) {
            indicator(position);
        }
    };

    private int mCurrentIndex = -1;

    private synchronized void indicator(int position) {
        if (mCurrentIndex != position) {
            mVpAfterSell.setCurrentItem(position);
            mAdapter = new MyOrderAdapter(this, mStringArray, mListener, position);
            mRvAfterSellHor.setAdapter(mAdapter);
            mCurrentIndex = position;
        }
    }

    @OnClick(R.id.iv_delete)
    public void onViewClicked() {

    }

    @Override
    public void onPageSelected(int position) {
        indicator(position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
